package by.koleda.photoupdater;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import by.koleda.photoupdater.helpers.ContextHolder;
import by.koleda.photoupdater.manager.ContactsManager;

public class ContactsAdapter extends BaseAdapter {
	
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	private Context context = ContextHolder.getInstance().getContext();
	
    public ContactsAdapter(ArrayList<HashMap<String, String>> data) {
        this.data = data;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.list_row, null);
		}
		
		TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
        TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
 
        HashMap<String, String> contact = new HashMap<String, String>();
        contact = data.get(position);
        
        Bitmap photo = null;
	
	    title.setText(contact.get(ContactsManager.KEY_DISPLAY_NAME));
	    artist.setText("Latest synchronization: 5 Jan 2013 00:30:25");
	    duration.setText(contact.get(ContactsManager.KEY_ID));
	    
	    photo = ContactsManager.getContactPhoto(contact.get(ContactsManager.KEY_ID));
	    if (photo != null) {
	    	thumb_image.setImageBitmap(photo);
	    }
	    
	    return vi;
	}
}
